package com.zzt.questionbankusermanager.service.impl;

import com.zzt.questionbankusermanager.bean.MenuItem;
import com.zzt.questionbankusermanager.bean.dto.MenuItemDTO;
import com.zzt.questionbankusermanager.bean.dto.MenuList;
import com.zzt.questionbankusermanager.bean.dto.PageResult;
import com.zzt.questionbankusermanager.bean.dto.Result;
import com.zzt.questionbankusermanager.bean.tools.BeanUtils;
import com.zzt.questionbankusermanager.bean.tools.MenuCreater;
import com.zzt.questionbankusermanager.mapper.MenuItemConverter;
import com.zzt.questionbankusermanager.repository.MenuItemRepository;
import com.zzt.questionbankusermanager.service.MenuItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MenuItemServiceImpl implements MenuItemService {
    @Autowired
    private MenuItemRepository menuItemRepository;

    @Autowired
    private MenuItemConverter menuItemConverter;

    @Override
    public MenuList getAll() {
        List<MenuItemDTO> dtos=menuItemConverter.doToDTO((List< MenuItem >)menuItemRepository.findAll());
        MenuCreater menuCreater=new MenuCreater();
        MenuList menuList= menuCreater.createMenu((long)1,dtos);
        return menuList;
    }

    @Override
    public List< MenuItemDTO > getMenuList() {
        List<MenuItem> dos=(List<MenuItem>)menuItemRepository.findAll();
        List<MenuItemDTO> dtos=menuItemConverter.doToDTO(dos);
        return dtos;
    }

    @Override
    public PageResult< MenuItemDTO > getAPage(PageRequest pageRequest) {
        Page<MenuItem> page=menuItemRepository.findAll(pageRequest);
        List<MenuItem> dos=page.getContent();
        List<MenuItemDTO> dtos=menuItemConverter.doToDTO(dos);
        PageResult pageResult=new PageResult();
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }

    @Override
    public PageResult< MenuItemDTO > getASpecialPage(Specification< MenuItem > specification, PageRequest pageRequest) {
        Page<MenuItem> page=menuItemRepository.findAll(specification,pageRequest);
        List<MenuItem> dos=page.getContent();
        List<MenuItemDTO> dtos=menuItemConverter.doToDTO(dos);
        PageResult pageResult=new PageResult();
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }

    @Override
    public Result addMenuItem(MenuItemDTO menuItemDTO) {
        Result result=new Result();
        if(menuItemRepository.findByAliasAndName(menuItemDTO.getAlias(),menuItemDTO.getName())==null){
            if(menuItemDTO.getIcon()==null||menuItemDTO.getAlias()==null||menuItemDTO.getName()==null){
                result.setMsg("参数不完整，添加失败");
                result.setCode(2);
            }
            else {
                MenuItem menuItem=menuItemConverter.dtoToDO(menuItemDTO);
                menuItem.setState("ENABLE");
                menuItemRepository.save(menuItem);
                result.setMsg("添加成功");
                result.setCode(1);
            }
        }
        else {
            result.setMsg("已存在同名菜单，添加失败");
            result.setCode(3);
        }
        return result;
    }

    @Override
    public Result editMenuItem(MenuItemDTO menuItemDTO) {
        Result result=new Result();
        if(menuItemDTO.getId()!=null){
            MenuItem menuItem=menuItemRepository.findById(menuItemDTO.getId()).get();
            if(menuItem==null){
                result.setMsg("对象不存在");
                result.setCode(4);
            }
            else {
                if(menuItemDTO.getState().equals("ENABLE")||menuItemDTO.getState().equals("DISABLE")){
                    MenuItem source= menuItemConverter.dtoToDO(menuItemDTO);
                    BeanUtils.copyProperties(source,menuItem);
                    menuItemRepository.save(menuItem);
                    result.setMsg("修改成功");
                    result.setCode(1);
                }
                else {
                    result.setMsg("参数不正确");
                    result.setCode(2);
                }
            }
        }
        else {
            result.setMsg("参数不正确");
            result.setCode(2);
        }
        return result;
    }

    @Override
    public Result deleteMenuItem(Long menuItemId) {
        Result result=new Result();
        if(menuItemRepository.findById(menuItemId).isPresent()){
            menuItemRepository.deleteById(menuItemId);
            result.setMsg("删除成功");
            result.setCode(1);
        }
        else {
            result.setMsg("对象不存在，删除失败");
            result.setCode(4);
        }
        return result;
    }


}
